package com.sharemarking.wa.platform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sharemarking.wa.common.entity.Agreement;
import com.sharemarking.wa.platform.dto.agreement.AgreementInfoDto;
import com.sharemarking.wa.platform.dto.agreement.OrgAgreementDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface AgreementMapper extends BaseMapper<Agreement> {

    @Select("SELECT id AS agreementId,name AS orgName,exam_state,reject_reason,expire_date FROM wa_agreement WHERE org_id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId}) " +
            "LIMIT #{pageIndex}, #{pageSize}")
    List<OrgAgreementDto> getOrgAgreementList(Map map);

    @Select("SELECT id AS agreementId,org_id,(SELECT IFNULL(a.real_name,a.name) FROM sys_org a WHERE a.id=org_id) AS orgName,exam_state,reject_reason,expire_date FROM wa_agreement WHERE name = #{name} " +
            "LIMIT #{pageIndex}, #{pageSize}")
    List<OrgAgreementDto> getOrgAgreementLists(Map map);

    @Select("SELECT COUNT(*) FROM wa_agreement WHERE org_id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId})")
    Integer getOrgAgreementListCount(Integer memberId);

    @Select("SELECT org_id FROM wa_agreement WHERE name = #{name} AND expire_date > now()")
    List<Integer> getOrgId(String name);

    @Select("SELECT a.expire_date,a.pics AS picsList,a.name AS manageOrgName,a.exam_state,a.reject_reason,IFNULL(b.real_name,b.name) AS produceOrgName " +
            "FROM wa_agreement a LEFT JOIN sys_org b ON a.org_id = b.id WHERE a.id = #{id} AND b.member_id = #{memberId}")
    AgreementInfoDto getAgreementInfo(@Param("id") Integer id, @Param("memberId") Integer memberId);

    @Select("SELECT a.expire_date,a.pics AS picsList,a.name AS manageOrgName,a.exam_state,a.reject_reason,IFNULL(b.real_name,b.name) AS produceOrgName " +
            "FROM wa_agreement a LEFT JOIN sys_org b ON a.org_id = b.id WHERE a.id = #{id}")
    AgreementInfoDto getAgreementInfoByManageId(@Param("id") Integer id, @Param("memberId") Integer memberId);

    @Select("SELECT COUNT(*) FROM wa_agreement WHERE id=#{id}")
    Integer isVerify(@Param("memberId") Integer memberId, @Param("id") Integer id);

    @Select("SELECT id AS agreementId,name AS orgName,exam_state,reject_reason,expire_date FROM wa_agreement WHERE org_id = #{orgId} and name = #{real_name} and exam_state = 1")
    OrgAgreementDto isOrgAgreementExpired(Integer orgId,String real_name);


}
